package com.wenju.inappupdatemodule.utils;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;

/**
 * 类描述：FileUtil
 *
 */
public class FileUtil {

    public static File updateDir = null;
    public static File updateFile = null;
    /***********
     * 保存升级APK的目录
     ***********/
    public static final String KonkaApplication = "UpdateApp";

    public static boolean isCreateFileSucess;

    /**
     * 方法描述：createFile方法
     *
     * @param app_name
     * @return
     * @see FileUtil
     */
    public static void createFile(String app_name) {

        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            isCreateFileSucess = true;

            updateDir = new File(getSDPath() + File.separator + KonkaApplication );
            updateFile = new File(updateDir + File.separator + app_name + ".apk");

            if (!updateDir.exists()) {
                updateDir.mkdirs();
            }

            if(updateDir.exists()){
                if (!updateFile.exists()) {
                    try {
                        updateFile.createNewFile();
                    } catch (IOException e) {
                        isCreateFileSucess = false;
                        e.printStackTrace();
                        Log.d("--------------->",e.toString());
                    }
                }
            }else{
                Log.d("---------------》","未找到文件夹");
            }
        } else {
            isCreateFileSucess = false;
        }
    }
    private static String getSDPath() {
        boolean hasSDCard = Environment.getExternalStorageState().equals("mounted");
        return hasSDCard ? Environment.getExternalStorageDirectory().toString() : Environment.getDownloadCacheDirectory().toString();
    }
}